var benchTable;
$(function(){
    $('#' + tabId + ' input').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%' // optional
    });
   /*	获取分组器版本号*/
	$.ajax({
		url:contextPath + '/benchmarkView/getGroupVersion.dhc',
		async:false,
		type:"post",
		success:function(result){
			if(result.success){
				var data = result.data;
				var str = "";
				if (data) {
					for ( var i in data) {
						str += "<option value='" + data[i].VER_CODE + "'>"+ data[i].VER_DESC + "</option>";
					}
					$('#' + tabId + ' #groupSelector').html(str);
					var verCode=$('#'+tabId+' #groupSelector').val();
					getBenchType(1,verCode);//获取年度标杆类型
				}else{
					$('#' + tabId + ' #groupSelector').html("<option value=''>请选择</option>");	
				}
			}
		}
	});
	//联动，当分组器版本号改变时，标杆类型和标杆年份相应重新查询
	$('#'+tabId+' #groupSelector').change(function(){
		var verCode=$('#'+tabId+' #groupSelector').val();
		getBenchType(1,verCode);//获取年度标杆类型
	});

    $("#"+tabId+" #year_id").on('ifChecked', function(event){
	//$("#"+tabId+" #year_id").click(function(){
		$("#"+tabId+" .bench_computer").css('display','block');
		$("#"+tabId+" .bench_std").css('display','none');
		$("#"+tabId+" .bench_manager").css('display','none');
		$("#"+tabId+" #benchdtil_year_id_wrapper").css('display','block');
		$("#"+tabId+" #benchdtil_manager_id_wrapper").css('display','none');
		$("#"+tabId+" #benchdtil_std_id_wrapper").css('display','none');
		$("#"+tabId+" #benchdtil_year_id").DataTable().columns.adjust();
		$('#'+tabId+' #groupSelector').change(function(){
			var verCode=$('#'+tabId+' #groupSelector').val();
			getBenchType(1,verCode);//获取年度标杆类型
		})
	});
    $("#"+tabId+" #manager_id").on('ifChecked', function(event){
	//$("#"+tabId+" #manager_id").click(function(){
		$("#"+tabId+" .bench_manager").css('display','block');
		$("#"+tabId+" .bench_computer").css('display','none');
		$("#"+tabId+" .bench_std").css('display','none');
		$("#"+tabId+" #benchdtil_year_id_wrapper").css('display','none');
		$("#"+tabId+" #benchdtil_manager_id_wrapper").css('display','block');
		$("#"+tabId+" #benchdtil_std_id_wrapper").css('display','none');
	//	$("#"+tabId+" #benchdtil_manager_id").DataTable().columns.adjust();
//		getBenchYears(2);
		var verCode=$('#'+tabId+' #managerObj').val();
		getBenchType(2,verCode);
		$('#'+tabId+' #managerObj').change(function(){
			verCode=$('#'+tabId+' #managerObj').val();
			getBenchType(2,verCode);//获取年度标杆类型
		})
	});
    $("#"+tabId+" #std_id").on('ifChecked', function(event){
	//$("#"+tabId+" #std_id").click(function(){
		$("#"+tabId+" .bench_std").css('display','block');
		$("#"+tabId+" .bench_computer").css('display','none');
		$("#"+tabId+" .bench_manager").css('display','none');
		$("#"+tabId+" #benchdtil_year_id_wrapper").css('display','none');
		$("#"+tabId+" #benchdtil_manager_id_wrapper").css('display','none');
		$("#"+tabId+" #benchdtil_std_id_wrapper").css('display','block');
	//	$("#"+tabId+" #benchdtil_std_id").DataTable().columns.adjust();
//		getBenchYears(3);
		getBenchType(3);
	});
	var benchObj=1;
	//单选框点击事件
	$("#"+tabId+" #stdId .radio_bg input").click(function(){
		benchObj=$(this).val();
	});
	//获取年度标杆
	$('#'+tabId+' #getStdBencahData').click(function(){
		getStdBenchData();
	});
	$('#'+tabId+' #getStdBencahData').click();
	//获取管理目标
	$('#'+tabId+' #getManagerBencahData').click(function(){
		getManagerBencahData();
	});
	//获取标杆比
	$('#'+tabId+' #getBencahData').click(function(){
		getBencahData();
	});
	
});
/**
 * @ Discription:获取年度标杆
 * @Autor ：liuhongjun
 * @Date  ：2018年3月12日下午6:30:01
 */
function getStdBenchData(){
	$.post(contextPath + '/benchmarkView/getStdBenchData.dhc',{
		verSion:$('#'+tabId+' #groupSelector').val(),
		stdYear:$('#'+tabId+' #yearSelector').val(),
	    stdType:$('#'+tabId+' #yearbenchType').val()
	}).done(function(result) {
		$('#'+tabId+' #checkin-detail').html("<table id='benchdtil_year_id' class='stripe cc table'></table>");
		var data=[];
		if(result.success&& result.data&&result.data.length>0){
			data=result.data;
		}
			var benchTable_year;
			var columns = [ {
				"data" : "DRG",
				"title":"病组代码",
				"render":function(obj, type, full){
					return full.AS_DRG_CODE+"-"+full.DRG_DESC;
				}
			}, {
				"data" : "AS_AVG_DAYS",
				"title":"平均住院日","render":function(obj, type, full){
           		 return  formatNumber(full.AS_AVG_DAYS,2)  ;
           	 }
			}, {
				"data" : "AS_AVG_FEE",
				"title":"次均费用","render":function(obj, type, full){
	           		 return  formatNumber(full.AS_AVG_FEE,2)  ;
	           	 }
			}, {
				"data" : "AS_DEAD_CNT",
				"title":"死亡人数"
			}, {
				"data" : "AS_RISK_LEVEL",
				"title":"风险等级"
			}, {
				"data" : "AS_TOTAL_CASE",
				"title":"病例数"
			}, {
				"data" : "AS_WEIGHT",
				"title":"权重","render":function(obj, type, full){
	           		 return  formatNumber(full.AS_WEIGHT,2)  ;
	           	 }
			}, {
				"data" : "PRIMEVAL_AVG_FEE",
				"title":"次均费用原始值","render":function(obj, type, full){
	           		 return  formatNumber(full.PRIMEVAL_AVG_FEE,2)  ;
	           	 }
			},{
				"data":"AS_DRG_MED_COST",
				"title":"医疗费标杆"
			},{
				"data":"AS_DRG_NURS_COST",
				"title":"护理费标杆"
			},{
				"data":"AS_DRG_TECH_COST",
				"title":"医技费标杆"
			},{
				"data":"AS_DRG_DRUG_COST",
				"title":"药耗费标杆"
			},{
				"data":"AS_DRG_MANAGE_COST",
				"title":"管理费标杆"
			},{
				"data":"AS_DRUG_RATIO",
				"title":"药占比",
				"render":function(obj, type, full){
	           		 return  formatNumber(full.AS_DRUG_RATIO,2)  ;
	           	 }
			},{
				"data":"AS_AVG_DRUG_FEE",
				"title":"例均药品费用"
			},{
				"data":"AS_AVG_MATERIAL_FEE",
				"title":"例均耗材费用"
			},{
				"data":"AS_MATERIAL_RATIO",
				"title":"耗材占比",
				"render":function(obj, type, full){
					return  formatNumber(full.AS_MATERIAL_RATIO)  ;
				}
			},{
				"data":"AS_DIED_RATIO",
				"title":"死亡率",
				"render":function(obj, type, full){
					return  formatNumber(full.AS_DIED_RATIO)  ;
				}
			}];
		var paramsDataTables = {
				stateSave : true,
				rownumbers : false,
				pageLength : 10,
				order : [ [ 0, "asc" ] ]
			};
		if(benchTable_year!=undefined){
			benchTable_year.destroy();
		}
		benchTable_year=$('#'+tabId+' #benchdtil_year_id').dataTableBasic(columns,data,paramsDataTables);
	
	});
}
/**
 * @ Discription:获取年度标杆
 * @Autor ：liuhongjun
 * @Date  ：2018年3月12日下午6:30:01
 */
function getManagerBencahData(){
	$.post(contextPath + '/benchmarkView/getManagerBencahData.dhc',{
		managerObj:$('#'+tabId+' #managerObj').val(),
		stdYear:$('#'+tabId+' #managerYearSelector').val(),
	    stdType:$('#'+tabId+' #managerbenchType').val()
	}).done(function(result) {
		$('#'+tabId+' #checkin-detail').html("<table id='benchdtil_manager_id' class='table'></table>");
		var data=[];
		if(result.success&& result.data &&result.data.length>0){
			data=result.data;
		}
		var benchTable_manager;
		var objtype=$('#'+tabId+' #managerObj').val();
		var columns ;
		if(objtype=='hosp'){
			  columns = [{
				"data" : "PARQUOTA_CODE",
				"title":"指标代码"
			},{
				"data" : "QUOTANAME",
				"title":"指标名称"
			},{
				"data" : "MANAGE_BENCHMARK",
				"title":"标杆值"
			}];
		}else if(objtype=='depGroup'){
			  columns = [{
					"data" : "OBJNAME",
					"title":"科室组"
				}];
		}else if(objtype=='dep'){
			  columns = [{
					"data" : "OBJNAME",
					"title":"科室"
				}];
		}else if(objtype=='drg'){
			  columns = [{
					"data" : "OBJNAME",
					"title":"DRG代码"
				}];
		}
		if(objtype!='hosp'){
			columns.push(
					{
				"data" : "AVGDAYS",
				"title":"平均住院日"
		    },{
				"data" : "AVGFEE",
				"title":"次均费用"
			},{
				"data" : "DRUGRATIO",
				"title":"药占比（%）"
			});
		}
		var paramsDataTables = {
				stateSave : true,
				rownumbers : false,
				pageLength : 10,
				order : [ [ 0, "asc" ] ]
			};
		if(benchTable_manager!=undefined){
			benchTable_manager.destroy();//先销毁
		}
		benchTable_manager=$('#'+tabId+' #benchdtil_manager_id').dataTableBasic(columns,data,paramsDataTables);
	
	});
}
/**
 * @ Discription:获取标杆比
 * @Autor ：liuhongjun
 * @Date  ：2018年3月12日下午8:15:53
 */
function getBencahData(){
	$.post(contextPath + '/benchmarkView/getBencahData.dhc',{
		stdYear:$('#'+tabId+' #stdYearSelector').val(),
	    stdType:$('#'+tabId+' #stdbenchType').val()
	}).done(function(result) {
		$('#'+tabId+' #checkin-detail').html("<table id='benchdtil_std_id' class='table'></table>");
		var data=[];
		if(result.success&& result.data &&result.data.length>0){
			data=result.data;
		}
		var benchTable_std;
		var columns= [{
				"data" : "DRGCODE",
				"title":"DRG代码"
			},{
				"data" : "AVGDAYS",
				"title":"平均住院日"
		    },{
				"data" : "AVGFEE",
				"title":"次均费用"
			},{
				"data" : "DRUGRATIO",
				"title":"药占比（%）"
			}];
		var paramsDataTables = {
				stateSave : true,
				rownumbers : false,
				pageLength : 10,
				order : [ [ 0, "asc" ] ]
			};
		if(benchTable_std!=undefined){
			benchTable_std.destroy();//先销毁
		}
		benchTable_std=$('#'+tabId+' #benchdtil_std_id').dataTableBasic(columns,data,paramsDataTables);
	
	});
}
/**
 * @ Discription:根据表格类型获取可用的标杆年份
 * @Autor ：liuhongjun
 * @Date  ：2018年3月12日上午11:17:33
 */
function getBenchYears(type,benchType,verCode){
	$.ajax({
		url:contextPath + '/benchmarkView/getBenchYears.dhc',
		data:{"type":type,"benchType":benchType,"verCode":verCode},
		type:"post",
		async:false,
		success:function(result){
			if(result.success){
				var data=result.data;
				if(data){
					var str='';
					for(var i in data){
						str += "<option value='" + data[i].BENCHYEAR + "'>"+ data[i].BENCHYEAR + "</option>";
					}
					if(type=='1'){
						$('#'+tabId+' #yearSelector').html(str);
					}else if(type=='2'){
						$('#'+tabId+' #managerYearSelector').html(str);
					}else if(type=='3'){
						$('#'+tabId+' #stdYearSelector').html(str);
					}
				}
			}
		}
	})
}
/**
 * @ Discription:获取标杆类型
 * @Autor ：liuhongjun
 * @Date  ：2018年3月12日下午2:30:17
 */
function getBenchType(type,verCode){
	var benchType = "";
	$.ajax({
		url:contextPath + '/benchmarkView/getBenchType.dhc',
		data:{"type":type,"verCode":verCode},
		async:false,
		type:"post",
		success:function(result){
			if(result.success){
				var data = result.data;
				if(data){
					var str = '';
					for(var i in data){
						str += '<option value="'+data[i].DIC_VALUE+'">'+data[i].DIC_DESC+'</option>';
					}
					if(type=='1'){
						$('#'+tabId+' #yearbenchType').html(str);
						 benchType = $('#'+tabId+' #yearbenchType').val();
					}else if(type=='2'){
						$('#'+tabId+' #managerbenchType').html(str);
						benchType = $('#'+tabId+' #managerbenchType').val();
					}else if(type=='3'){
						$('#'+tabId+' #stdbenchType').html(str);
						benchType = $('#'+tabId+' #stdbenchType').val();
					}
					getBenchYears(type,benchType,verCode);
				}
			}
			
		}
	})
	//联动，当条件改变时
	$('#'+tabId+' #yearbenchType').change(function(){
		benchType = $('#'+tabId+' #yearbenchType').val();
		getBenchYears(type,benchType,verCode);
	});
	$('#'+tabId+' #managerbenchType').change(function(){
		benchType = $('#'+tabId+' #managerbenchType').val();
		getBenchYears(type,benchType,verCode);
	});
	$('#'+tabId+' #stdbenchType').change(function(){
		benchType = $('#'+tabId+' #stdbenchType').val();
		getBenchYears(type,benchType,verCode);
	});
}